part of 'view.dart';

final _dutyList = ValueNotifier(<DutyMgmtDayEntity>[]);
final _floodCtrl = ValueNotifier(<GetFloodPreventionDataByDutyEntity>[]);
final isNotPermissions = ValueNotifier(true);
/// 获得当天值班记录
Future<void> _getDutyList(String date) async {
  final resp = await HttpUtil().generateGetRequest<List<DutyMgmtDayEntity>>(
    '${Interface.mainBaseUrl}/admin-api/system/duty-record-info/queryDayRecord',
    queryParameters: {
      'dutyDate': date,
      'companyId': UserManager.getUserCompanyId(),
    },
  ).execute();
  if (resp.isSucceed) {
    isNotPermissions.value=true;
    final data = resp.data;
    if (data == null) return;
    _dutyList.value = data;
    // final weather = json.decode(data.firstOrNull?.weatherInfo ?? '{}');
    // if (weather is Map<String, String> && weather.isNotEmpty) {
    //   _weather = weather;
    // }
    // final floods = <_FloodCtrl>[];
    // for (final item in data) {
    //   final floodCtrl = item.floodPreventionInfo;
    //   if (floodCtrl != null) {
    //     final floodList = json.decode(floodCtrl) as List;
    //     for (final flood in floodList) {
    //       final ctrl = _FloodCtrl.fromJson(flood as Map<String, dynamic>);
    //       // 防止重复添加
    //       if (!floods.any((element) => element.reservoir == ctrl.reservoir)) {
    //         floods.add(ctrl);
    //       }
    //     }
    //     // final ctrl = _FloodCtrl.fromJson(json.decode(floodCtrl) as Map<String, dynamic>);
    //     // // 防止重复添加
    //     // if (!floods.any((element) => element.reservoir == ctrl.reservoir)) {
    //     //   floods.add(ctrl);
    //     // }
    //   }
    // }
    // _floodCtrl.value = floods;
    if(_dutyList.value.isNotEmpty){
      //获取值班记录的防汛的时分秒
      scheduleTime(_dutyList.value.first.companyId,date);
    }

  }
  resp.onError((result) {
    if(result.code==403&&result.message=='没有该操作权限'){
      isNotPermissions.value=false;
    }
  });
}
List<ScheduleTimeEntity> _scheduleTime = [];
/// 获得值班记录信息分页-获取时分秒
Future<void> scheduleTime(int? companyId, String date) async {
  final resp = await HttpUtil().generateGetRequest<List<ScheduleTimeEntity>>(
    '${Interface.mainBaseUrl}/admin-api/system/schedule_time/page',
    queryParameters: {
      'companyId': companyId,//公司id
      'timeType':3,//时间类型 1.值班信息弹出时间 2.防汛重点部位数据信息弹出时间 3.防汛监测时间
    },
  ).execute();
  if (resp.isSucceed) {
    if (  resp.data == null) return;
    _scheduleTime  = resp.data!;
    //年月日+时分秒（具体的时间点） 默认选第一个
    if(resp.data!.isNotEmpty){
      resp.data!.first.isOnClick = true;
      getFloodPreventionDataByDuty(companyId,'$date ${resp.data!.first.value!.split(' ')[1]}');
    }
  }
}

/// 获取防汛重点数据
Future<void> getFloodPreventionDataByDuty(int? companyId, String date) async {
  final resp = await HttpUtil().generateGetRequest<List<GetFloodPreventionDataByDutyEntity>>(
    '${Interface.mainBaseUrl}/admin-api/system/duty-record-date/getFloodPreventionDataByDuty',
    queryParameters: {
      'dataTime': date,
      'companyId': companyId,
    },
  ).execute();
  if (resp.isSucceed) {
    final data = resp.data;
    if (data == null) return;
    _floodCtrl.value = data;
  }
}

final _majorEvent = ValueNotifier(<MajorEventEntity>[]);

/// 获得重大事件信息列表
Future<void> _getMajorEvent(List<String> ids) async {
  if (ids.isEmpty || !ids.any((e) => e.isNotEmpty)) return;
  ids = ids.toSet().toList();
  final resp = await HttpUtil().generateGetRequest<List<MajorEventEntity>>(
    '${Interface.mainBaseUrl}/admin-api/system/major-event-info/getList',
    queryParameters: {'ids': ids.join(',')},
  ).execute();
  if (resp.isSucceed && resp.data != null) {
    _majorEvent.value = resp.data!;
  }
}

// var _weather = {
//   'vis': '24',
//   'cloud': '25',
//   'fxDate': '2024-04-08',
//   'precip': '0.0',
//   'sunset': '18:47',
//   'iconDay': '101',
//   'moonset': '18:25',
//   'sunrise': '05:47',
//   'tempMax': '22',
//   'tempMin': '8',
//   'textDay': '多云',
//   'uvIndex': '7',
//   'humidity': '36',
//   'moonrise': '05:30',
//   'pressure': '1020',
//   'iconNight': '305',
//   'moonPhase': '残月',
//   'textNight': '小雨',
//   'wind360Day': '90',
//   'windDirDay': '东风',
//   'wind360Night': '180',
//   'windDirNight': '南风',
//   'windScaleDay': '1-3',
//   'windSpeedDay': '16',
//   'moonPhaseIcon': '807',
//   'windScaleNight': '1-3',
//   'windSpeedNight': '3'
// };

// {
//   "reservoir": "栗批西沟尾矿库",
//   "rainfall": 38,
//   "waterLevel": 42,
//   "mainDamBeachDistance": 42,
//   "subDamBeachDistance": 38,
// }
final class _FloodCtrl {
  final String? reservoir;
  final int? rainfall;
  final int? waterLevel;
  final int? mainDamBeachDistance;
  final int? subDamBeachDistance;

  _FloodCtrl({
    this.reservoir,
    this.rainfall,
    this.waterLevel,
    this.mainDamBeachDistance,
    this.subDamBeachDistance,
  });

  factory _FloodCtrl.fromJson(Map<String, dynamic> json) {
    final info = json['info'][0] ?? {};
    return _FloodCtrl(
      reservoir: json['reservoirName'] as String?,
      rainfall: info['rainfall'] as int?,
      waterLevel: info['waterElevation'] as int?,
      mainDamBeachDistance: json['mainDamBeachDistance'] as int?,
      subDamBeachDistance: json['subDamBeachDistance'] as int?,
    );
  }
}
